package HighLevelFunction

import scala.collection.immutable

object ComplexWordCount1 {
  def main(args: Array[String]): Unit = {
    //基于与统计的结果进行转换
    val stringList: List[(String, Int)] = List(
      ("hello", 1),
      ("hello world", 4),
      ("hello scala", 3),
      ("hello spark", 2),
      ("hello spark from scala", 1),
      ("hello flink from scala", 2)
    )
    val newArrayList: List[(String, Int)] = stringList.flatMap(
      //按照第一个空格差分开，形成元组
      tupleList => {
        val strings: Array[String] = tupleList._1.split(" ")
        strings.map(
          word => {
            (word, tupleList._2)
          }
        )

      }
    )
    println(newArrayList)
    val stringGroupBy: Map[String, List[(String, Int)]] = newArrayList.groupBy(
      elem => {
        elem._1
      }
    )
    println(stringGroupBy)
    //    val countString: Map[String, Int] = stringGroupBy.mapValues(
    //      tuplesList => {
    //        tuplesList.map(
    //          e => e._2
    //        ).sum
    //      }
    //    )
    val countString: Map[String, Int] = stringGroupBy.map(
      tuplesList => {
        val sum: Int = tuplesList._2.map(
          e => e._2
        ).sum
        (tuplesList._1, sum)
      }
    )
    println(countString)
    val sort: List[(String, Int)] = countString.toList.sortBy(_._2).take(4)
    println(sort)


  }

}
